<template>
  <div class="login-page">
    <van-nav-bar
  title="账号登录"
  @click="onSubmit"
/>
    <van-form @submit="onSubmit">
      <van-field
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' },{pattern:/\w{5,}/,message:'用户名不能少于五位'}]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' },,{pattern:/\w{6,}/,message:'密码长度不能少于6位'}]"
  />
  <div style="margin: 16px;">
    <van-button  block type="primary" native-type="submit">登录</van-button>
  </div>
</van-form>
<div  class="link"><span @click="$router.push('/Register')">还没有账号去注册~</span>  </div>
  </div>
</template>

<script>
import instance from '../utils/request'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await instance.post('/user/login', values)
      console.log(res.data)
      if (res.data.status === 200) {
        this.$toast(res.data.description)
        localStorage.setItem('vue-hkzf-token', res.data.body.token)
        setTimeout(() => {
          this.$router.push('/article')
        }, 300)
      } else {
        this.$toast(res.data.description)
      }
    }

  }
}
</script>

<style lang="less" scoped>

.link {
  color: #069;
  font-size: 14px;
  padding-right: 20px;
 text-align: center;
}
</style>
